package com.mop.app.service;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class SmsService {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 发送短信
     * @param phone
     * @param smsType
     */
    public void sendSms(String phone, String smsType) {
        //默认

        redisTemplate.opsForValue().set(phone + smsType, "888888");
        redisTemplate.expire(phone + smsType, 300, TimeUnit.SECONDS);
    }

    /**
     * 校验手机号
     * @param phone
     * @param code
     * @param smsType
     * @return
     */
    public Boolean verifyCode(String phone, String code, String smsType) {
        Object cacheObj = redisTemplate.opsForValue().get(phone + smsType);
        return cacheObj == null ? false : code.equals(cacheObj.toString());
    }
}
